package com.freecat.database;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

public class SQLFunction {


    static DBHelper helper;


    public static void initTable(Context context){
        helper = new DBHelper(context);
        helper.getReadableDatabase();
    }

    /**【插入数据】**/
    public static void insert(Context context , Object[] data, String sql){

        Log.i("TAG:","插入数据到数据库表：:"+data.toString());

        DBManager sqlManager = new DBManager(context);
        helper = new DBHelper(context);
        helper.getWritableDatabase();
        Object[] bindArgs = data;
       boolean b=sqlManager.updateSQLite( sql , bindArgs );
       if (b){
//           helper.close();
           Log.i("TAG:","成功====insert");

       }else {

           Log.i("TAG:","数据库错误，无法读取数据====insert");

       }
    }


    /**【模糊查询】**/
    public static ArrayList<HashMap<String, String>> query(Context context, String where1 , String where2, String sql){
        DBManager sqlManager = new DBManager(context);
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
//        String sql = "select * from person where name like ? and info like ?";
        if(where1 == null){
            list = sqlManager.querySQLite(sql,new String[] {"%","%"});
        }else{
            where1 = "%" + where1 + "%";
            where2 = "%" + where2 + "%";
            list = sqlManager.querySQLite(sql,new String[] { where1 , where2 } );
        }

        Log.i("TAG:","查询完毕，返回数据：" + list.size());

        return list;
    }


    /**【模糊查询】**/
    public static ArrayList<HashMap<String, String>> query_get_time(Context context, String where1 , String where2, String sql){
        DBManager sqlManager = new DBManager(context);
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
//        String sql = "select * from person where name like ? and info like ?";
//        if(where1 == null){
//            list = sqlManager.querySQLite(sql,new String[] {"%","%"});
//        }else{
////            where1 = "%" + where1 + "%";
////            where2 = "%" + where2 + "%";
//            list = sqlManager.querySQLite(sql,new String [] { where1 , where2 } );
//        }
        list = sqlManager.querySQLite(sql,new String[] { where1 , where2 } );
        Log.i("TAG:","查询完毕，返回数据：" + list.size());




            return list;
    }

    public static ArrayList<HashMap<String, String>> query_get_all(Context context, String sql){
        DBManager sqlManager = new DBManager(context);
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
//        String sql = "select * from person where name like ? and info like ?";
//        if(where1 == null){
//            list = sqlManager.querySQLite(sql,new String[] {"%","%"});
//        }else{
////            where1 = "%" + where1 + "%";
////            where2 = "%" + where2 + "%";
//            list = sqlManager.querySQLite(sql,new String [] { where1 , where2 } );
//        }
        list = sqlManager.querySQLite(sql,null );
        Log.i("TAG:","查询完毕，返回数据：" + list.size());




        return list;
    }

    /**【删除数据】**/
    public static void delete(Context context , Object[] data, String sql){
        DBManager sqlmanager = new DBManager(context);
//        String sql = "delete from person where _id =  ? ";
        Object[] bindArgs = data;

        boolean b=sqlmanager.updateSQLite(sql , bindArgs);
        if (b){
        }else {
            Log.i("TAG:","数据库错误，无法读取数据====delete");

        }
    }

    /**【更新数据】**/
    public static void update(Context context , Object[]data, String sql){
        helper = new DBHelper(context);
        helper.getReadableDatabase();
        DBManager sqlManager = new DBManager(context);
//        String sql = "update person set name=? , info=? where _id=?";
        Object[] bindArgs = data;

        boolean b= sqlManager.updateSQLite(sql,bindArgs);
        if (b){
            helper.close();;

        }
    }


    /**【初始化数据库】**/
    public static void rest_db(Context context , Object[] data, String sql){
        DBManager sqlmanager = new DBManager(context);
//        String sql = "delete from person where _id =  ? ";
        boolean b=sqlmanager.updateSQLite(sql , data);
        if (b){
        }else {
            Log.i("TAG:","数据库错误，无法读取数据====delete");

        }
    }
}
